/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.client.goods.logic;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import mscs.data.IFileDataStore;
import mscs.data.IOperationProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.Operation;

/**
 *
 * @author Usbuild
 */
public abstract class OperationUtil implements IOperationUtil{
    private ArrayList<Operation> operationList = new ArrayList<Operation>();
    private IFileDataStore fds;
    
    public OperationUtil(String fileName) {
        fds = new IOperationProxy(fileName);
        operationList = (ArrayList<Operation>)fds.getObjects();
    }
    public ArrayList<Operation> queryByExample(Goods goods) {
        ArrayList<Operation> opList = new ArrayList<Operation>();
        for(Operation operation : operationList) {
            for(Goods preGoods : (ArrayList<Goods>)operation.getGoodsList())
                if(goods.getId().equals(preGoods.getId())) {
                    opList.add(operation);
                    break;
                }
        }
        return opList;
    }
    public void add(Operation operation) {
        operationList.add(operation);
        fds.update(operationList);
    }
    public Vector<Vector<String>> toDataTable() {
        try {
            Vector<Operation> seList = new Vector(operationList);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Operation operation : seList)
            {
                reList.add((Vector<String>)toStringArray(operation));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }
    public Vector getGoodsList(int index) {
        return new Vector(operationList.get(index).getGoodsList());
    }
    public List toStringArray(Operation op) {
        Vector<String> list = new Vector<String>();
        list.add(new SimpleDateFormat("yyyy-MM-dd").format(op.getDate()));
        list.add(((Integer)op.getOperator().getId()).toString());
        return list;
    }
   public List find(Operation obj) {
        Vector<Operation> reList = new Vector<Operation>();
        for(Operation op : operationList)
            if (op.getId() == obj.getId() )
                reList.add(op);
        return reList;
    }
}
